
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/mf4j/

//
// 091116a- aha42 - Starts in line change tracking.
//

package org.aha.mf4j.flickrutil;

import org.aha.mf4j.FlickrException;
import org.aha.mf4j.FlickrSession;
import org.aha.mf4j.response.FlickrObject;
import org.aha.mf4j.response.FlickrResponseException;

/**
 * <p>
 *   Utility methods of use when query Flickr on information about an user.
 * </p>
 * @author Arne Halvorsen (aha42)
 */
public final class People 
{
  private People(){} // Utility pattern dictates private constructor.
  
  /**
   * <p>
   *   Gets user's Flickr id (nsid) given user's Flickr user name.
   * </p>
   * @param name User name.
   * @param fs   Session to use.
   * @return User id (nsid) or {@code null} if user named is not found.
   * @throws FlickrException If fails.
   */
  public static String getNsidForUserName(String name, FlickrSession fs) 
    throws FlickrException
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    if (fs==null)
    {
      throw new NullPointerException("fs");
    }
    
    FlickrObject fo=fs.createRequest("flickr.people.findByUsername")
      .setArg("username", name).perform(false);
    if (fo.getMandatoryAttribute("stat").equals("fail"))
    {
      FlickrObject errFo=fo.getMandatoryChild("err");
      if (errFo.getMandatoryAttribute("code").equals("1")) return null;
      throw new FlickrResponseException(errFo);
    }
    
    FlickrObject userFo=fo.getMandatoryChild("user");
    return userFo.getMandatoryAttribute("nsid");
  }
  
  /**
   * <p>
   *   Gets user's Flickr id (nsid) given user's Flickr user registered email
   *   address.
   * </p>
   * @param email User's email address.
   * @param fs   Session to use.
   * @return User id (nsid) or {@code null} if email address is not found.
   * @throws FlickrException If fails.
   */
  public static String getNsidForEmail(String email, FlickrSession fs)
    throws FlickrException
  {
    if (email==null)
    {
      throw new NullPointerException("email");
    }
    if (fs==null)
    {
      throw new NullPointerException("fs");
    }
    
    FlickrObject fo=fs.createRequest("flickr.people.findByEmail")
      .setArg("find_email", email).perform(false);
    if (fo.getMandatoryAttribute("stat").equals("fail"))
    {
      FlickrObject errFo=fo.getMandatoryChild("err");
      if (errFo.getMandatoryAttribute("code").equals("1")) return null;
      throw new FlickrResponseException(errFo);
    }
    
    FlickrObject userFo=fo.getMandatoryChild("user");
    return userFo.getMandatoryAttribute("nsid");
  }
  
}
